package com.tledu.instrument.service.impl;

import com.tledu.common.utils.DateUtils;
import com.tledu.common.utils.SecurityUtils;
import com.tledu.instrument.domain.InstrumentInfo;
import com.tledu.instrument.domain.InstrumentLog;
import com.tledu.instrument.domain.InstrumentUse;
import com.tledu.instrument.domain.vo.InstrumentUseVo;
import com.tledu.instrument.mapper.InstrumentInfoMapper;
import com.tledu.instrument.mapper.InstrumentLogMapper;
import com.tledu.instrument.mapper.InstrumentUseMapper;
import com.tledu.instrument.service.IInstrumentInfoService;
import com.tledu.instrument.service.IInstrumentUseService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * 仪器使用记录Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-29
 */
@Service
public class InstrumentUseServiceImpl implements IInstrumentUseService {
    @Autowired
    private InstrumentUseMapper instrumentUseMapper;
    @Autowired
    private IInstrumentInfoService instrumentInfoService;
    @Autowired
    private InstrumentLogMapper instrumentLogMapper;
    @Autowired
    private InstrumentInfoMapper instrumentInfoMapper;

    /**
     * 查询仪器使用记录
     *
     * @param id 仪器使用记录主键
     * @return 仪器使用记录
     */
    @Override
    public InstrumentUseVo selectInstrumentUseById(Long id) {
        return instrumentUseMapper.selectInstrumentUseById(id);
    }

    /**
     * 查询仪器使用记录列表
     *
     * @param instrumentUse 仪器使用记录
     * @return 仪器使用记录
     */
    @Override
    public List<InstrumentUse> selectInstrumentUseList(InstrumentUse instrumentUse) {
//        instrumentUse.setCreateId(SecurityUtils.getUserId());
//        instrumentUse.setCreateTime(DateUtils.getNowDate());
        return instrumentUseMapper.selectInstrumentUseList(instrumentUse);
    }

    /**
     * 新增仪器使用记录
     *
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertInstrumentUse(Long id) {
        //1.插入仪器使用记录表
        InstrumentUse instrumentUse = new InstrumentUse();
        instrumentUse.setCreateTime(DateUtils.getNowDate());
        instrumentUse.setInstrumentId(id);
        instrumentUse.setCreateUser(SecurityUtils.getUserId());
        int n = instrumentUseMapper.insertInstrumentUse(instrumentUse);
        //2.修改仪器状态
        InstrumentInfo instrumentInfo = new InstrumentInfo();
        instrumentInfo.setId(id);
        instrumentInfo.setUseStatus(1L);
        int m =instrumentInfoMapper.updateInstrumentInfo(instrumentInfo);
        //3.新增到日志表
        InstrumentLog instrumentLog = new InstrumentLog();
        instrumentLog.setInstrumentId(instrumentInfo.getId());
        instrumentLog.setUpdateTime(DateUtils.getNowDate());
        instrumentLog.setCreateUser(SecurityUtils.getUserId());
        instrumentLog.setOperationType(3L);
        Date date = new Date();
        instrumentLog.setCreateTime(date);
        int i = instrumentLogMapper.insertInstrumentLog(instrumentLog);
        //4.判断受影响的行数
        if (n > 0 && m > 0 && i > 0) {
            return 1;
        } else {
            return 0;
        }
    }

    /**
     * 修改仪器使用记录
     *
     * @param instrumentUse 仪器使用记录
     * @return 结果
     */
    @Override
    public int updateInstrumentUse(InstrumentUse instrumentUse) {
        instrumentUse.setCreateId(SecurityUtils.getUserId());
        return instrumentUseMapper.updateInstrumentUse(instrumentUse);
    }

    /**
     * 批量删除仪器使用记录
     *
     * @param ids 需要删除的仪器使用记录主键
     * @return 结果
     */
    @Override
    public int deleteInstrumentUseByIds(Long[] ids) {
        return instrumentUseMapper.updateInstrumentUseByIds(ids);
    }



    /**
     * 使用记录中进行入库操作
     * @param instrumentUse
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateStatus(InstrumentUse instrumentUse) {

        //更新仪器表状态为0 未出库
        InstrumentInfo instrumentInfo = new InstrumentInfo();
        instrumentInfo.setId(instrumentUse.getInstrumentId());
        instrumentInfo.setUseStatus(instrumentUse.getUseStatus());
        int i = instrumentInfoMapper.updateInstrumentInfo(instrumentInfo);

        //新增到日志表
        InstrumentLog instrumentLog = new InstrumentLog();
        instrumentLog.setInstrumentId(instrumentInfo.getId());
        instrumentLog.setUpdateTime(DateUtils.getNowDate());
        instrumentLog.setCreateUser(SecurityUtils.getUserId());
        instrumentLog.setOperationType(4L);
        instrumentLog.setCreateTime(new Date());
        instrumentLogMapper.insertInstrumentLog(instrumentLog);

        return i;
    }

}
